<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>用户授权</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8"/>
    <link rel="stylesheet" href="../../css/font.css">
    <link rel="stylesheet" href="../../css/xadmin.css">
    <link rel="stylesheet" href="../../lib/layui/css/layui.css" media="all">
    <script src="../../lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../../js/xadmin.js"></script>

    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>



<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <div class="am-cf am-padding am-padding-bottom-0">

                        <form class="layui-form layui-col-space5">
                            <div class="x-nav">
          <span class="layui-breadcrumb">
            <a href="">首页</a>

            <a>
              <cite>用户及授权/Users and authorization</cite></a>
          </span>
                                <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
                                   onclick="location.reload()" title="刷新">
                                    <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
                            </div>
                            <br>
                            <div class="layui-inline layui-show-xs-block">
                                <input type="text" id="uLoginName" placeholder="请输入用户登录名" autocomplete="off"
                                       class="layui-input">
                            </div>
                            <div class="layui-inline layui-show-xs-block">
                                <button class="layui-btn" lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i>
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
                <div class="layui-card-header demoTable">
                    <button class="layui-btn" onclick="xadmin.open('新增用法','./6.10-add.html',600,400)"><i
                            class="layui-icon"></i>添加
                    </button>
                    <button class="layui-btn layui-btn-danger" data-type="getCheckData"><i class="layui-icon"></i>批量删除
                    </button>
                </div>

                <div class="layui-card-body layui-table-body layui-table-main">
                    <table class="layui-table layui-form" id="demo" lay-filter="demo">
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script>
    layui.use(['laydate', 'laypage', 'layer', 'table', 'carousel', 'upload', 'element', 'slider', 'form'], function () {
        var laydate = layui.laydate //日期
            , laypage = layui.laypage //分页
            , layer = layui.layer //弹层
            , table = layui.table //表格
            , carousel = layui.carousel //轮播
            , upload = layui.upload //上传
            , element = layui.element //元素操作
            , slider = layui.slider //滑块
            , table = layui.table
            , form = layui.form

        //获取地址栏数据
        function getName(name, sign) {
            if (!sign) {
                var sign = '&'
            }
            var r = top.location.href;
            var sub = r.split(sign)
            for (var i = 0; i < sub.length; i++) {
                if (sub[i].indexOf(name) >= 0) {
                    if (sub[i].indexOf('=')) {
                        var data = sub[i].substring(sub[i].indexOf('=') + 1)
                        return data
                    }
                }
            }
        }

        console.log(getName('uLoginName'));
        //监听提交（查询用法）
        form.on('submit(sreach)', function () {
            var uLoginName = $('#uLoginName').val();
            $.ajax({
                url: '/JT90/user/selectUserByNameOrPinyinInitials',//地址
                dataType: 'json',//数据类型
                data: {"uLoginName": uLoginName},
                type: 'GET',//类型
                contentType: 'application/json;charset=UTF-8',
                //请求成功
                success: function () {
                    //分页
                    table.render({
                        elem: '#demo'
                        , id: 'test'
                        , height: 300
                        , url: '/JT90/user/selectUserByNameOrPinyinInitials?uLoginName=' + uLoginName //数据接口
                        , page: {
                            count: 100
                            , layout: ['limit', 'count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
                            , groups: 5 //只显示 1 个连续页码
                            , first: '首页'//不显示首页
                            , last: '尾页' //不显示尾页
                            , limit: 5
                            , limits: [5, 10, 15, 20]
                            , theme: '#44cc33'
                            , jump: function (obj) {
                                console.log(obj)
                            }
                        }//开启分页
                        , cols: [[ //表头
                            {type: 'checkbox', width: '10%', fixed: 'left'}
                            , {field: 'uId', title: '用户编号', width: '10%'}
                            , {field: 'tId', title: '角色编号', width: '10%'}
                            , {field: 'tIdShow', title: '角色', width: '10%'}
                            , {field: 'uEntryDate', title: '用户入职时间', width: '20%'}
                            , {field: 'uQuitDate', title: '用户离职时间', width: '20%'}
                            , {field: 'departIdShow', title: '部门', width: '10%'}
                            , {field: 'officesIdShow', title: '科室', width: '10%'}
                            , {field: 'uStartDate', title: '用户上班时间', width: '20%'}
                            , {field: 'uStopDate', title: '用户下班时间', width: '20%'}
                            , {field: 'uModifier', title: '修改人', width: '10%'}
                            , {field: 'uModifyDate', title: '修改时间', width: '20%'}
                            , {field: 'uLoginName', title: '用户登录名', width: '10%'}
                            , {field: 'uCardId', title: '用户身份证', width: '20%'}
                            , {field: 'uAddress', title: '用户住址', width: '20%'}
                            , {field: 'uPhone', title: '用户联系电话', width: '20%'}
                            , {field: 'uBirthday', title: '用户出生日期', width: '20%'}
                            , {field: 'uAge', title: '用户年龄', width: '10%'}
                            , {field: 'uGender', title: '用户性别', width: '10%'}
                            , {field: 'uName', title: '用户姓名', width: '10%'}
                            , {
                                field: 'right', title: '状态', width: '10%', fixed: 'right', templet: function (d) {
                                    var state = "";
                                    if (d.uState == "15") {
                                        if (d.tId === 6) {
                                            state = "<input type='checkbox' value='" + d.uId + "' id='status' lay-filter='stat' checked='checked' name='status'  lay-skin='switch' lay-text='启用|启用' disabled>";
                                        } else {
                                            state = "<input type='checkbox' value='" + d.uId + "' id='status' lay-filter='stat' checked='checked' name='status'  lay-skin='switch' lay-text='启用|停用'>";
                                        }
                                    } else {
                                        state = "<input type='checkbox' value='" + d.uId + "' id='status' lay-filter='stat'  name='status'  lay-skin='switch' lay-text='启用|停用' >";
                                    }
                                    return state;
                                }
                            }
                            , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: '15%'}
                        ]]
                        , even: true
                    });
                }
            });

            return false;
        });
        //分页
        table.render({
            elem: '#demo'
            , id: 'test'
            , height: 300
            , url: '/JT90/user/selectUsersList' //数据接口
            , page: {
                count: 100
                , layout: ['limit', 'count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
                , groups: 5 //只显示 1 个连续页码
                , first: '首页'//不显示首页
                , last: '尾页' //不显示尾页
                , limit: 5
                , limits: [5, 10, 15, 20]
                , theme: '#44cc33'
                , jump: function (obj) {
                    console.log(obj)
                }
            }//开启分页
            , cols: [[ //表头
                {
                    type: 'checkbox', width: '10%', fixed: 'left'
                }
                , {field: 'uId', title: '用户编号', width: '10%'}
                , {field: 'tId', title: '角色编号', width: '10%'}
                , {field: 'tIdShow', title: '角色', width: '10%'}
                , {field: 'uEntryDate', title: '用户入职时间', width: '20%'}
                , {field: 'uQuitDate', title: '用户离职时间', width: '20%'}
                , {field: 'departIdShow', title: '部门', width: '10%'}
                , {field: 'officesIdShow', title: '科室', width: '10%'}
                , {field: 'uStartDate', title: '用户上班时间', width: '20%'}
                , {field: 'uStopDate', title: '用户下班时间', width: '20%'}
                , {field: 'uModifier', title: '修改人', width: '10%'}
                , {field: 'uModifyDate', title: '修改时间', width: '20%'}
                , {field: 'uLoginName', title: '用户登录名', width: '10%'}
                , {field: 'uCardId', title: '用户身份证', width: '20%'}
                , {field: 'uAddress', title: '用户住址', width: '20%'}
                , {field: 'uPhone', title: '用户联系方式', width: '20%'}
                , {field: 'uBirthday', title: '用户出生日期', width: '20%'}
                , {field: 'uAge', title: '用户年龄', width: '10%'}
                , {field: 'uGender', title: '用户性别', width: '10%'}
                , {field: 'uName', title: '用户姓名', width: '10%'}
                , {
                    field: 'right', title: '状态', width: '10%', fixed: 'right', templet: function (d) {
                        var state = "";
                        if (d.uState == "15") {
                            if (d.tId === 6) {
                                state = "<input type='checkbox' value='" + d.uId + "' id='status' lay-filter='stat' checked='checked' name='status'  lay-skin='switch' lay-text='启用|停用' disabled>";
                            } else {
                                state = "<input type='checkbox' value='" + d.uId + "' id='status' lay-filter='stat' checked='checked' name='status'  lay-skin='switch' lay-text='启用|停用'>";
                            }
                        } else {
                            state = "<input type='checkbox' value='" + d.uId + "' id='status' lay-filter='stat'  name='status'  lay-skin='switch' lay-text='启用|停用' >";
                        }
                        return state;
                    }
                }
                , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: '18%'}
            ]]
            , even: true
        });

        //监听工具条
        table.on('tool(demo)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
            if (layEvent === 'del') {
                layer.confirm('确认删除这条数据吗？', function (index) {
                    var uId = data.uId;//获取选中行的对象的id值
                    //发异步删除数据
                    $.ajax({
                        url: '/JT90/user/deleteUserById',//地址
                        dataType: 'json',//数据类型
                        data: {"uId": uId},
                        type: 'GET',//类型
                        contentType: 'application/json;charset=UTF-8',
                        //请求成功
                        success: function (map) {
                            if (map.msg) {
                                $(obj).parents("tr").remove();
                                if (map.msg) {
                                    layer.msg('已删除!', {
                                        icon: 1,
                                        time: 1000
                                    });
                                }

                            }
                        }
                    });
                    /*用户-删除*/
                    obj.del();
                    layer.close(index);

                });
            } else if (obj.event === 'authority') {
                var uId = data.uId;//获取选中行的对象的id值
                var tId = data.tId;
                layer.open({
                    type: 2,
                    area: ['600px', '400px'],
                    title: '修改权限',
                    content: 'http://122.51.140.205:8892/JT90/index/6BasicArchives/6.10-authority.html',
                    success: function () {
                        var body = layer.getChildFrame('body');//获取弹窗页面
                        body.find('#uId').val(uId);
                        body.find('#tId').val(uId);
                    }

                });
            } else if (obj.event === 'edit') {
                var data = obj.data; //获得当前行数据
                /* var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）*/
                var uId = data.uId;//获取选中行的对象的id值
                var uModifier = (getName("uLoginName"));
                var uLoginName = data.uLoginName;
                var uPassword = data.uPassword;
                var officesId = data.officesId;
                var departId = data.departId;
                var uEntryDate = data.uEntryDate;
                var uStartDate = data.uStartDate;
                var uStopDate = data.uStopDate;
                var uName = data.uName;
                var uAge = data.uAge;
                var uCardId = data.uCardId;
                var uPhone = data.uPhone;
                var uAddress = data.uAddress;
                var uBirthday = data.uBirthday;
                var uGender = data.uGender;
                var tId = data.tId;

                //编辑弹窗事件
                layer.open({
                    type: 2,
                    area: ['600px', '400px'],
                    title: '修改用户',
                    content: 'http://122.51.140.205:8892/JT90/index/6BasicArchives/6.10-edit.html?uId=' + uId + '&uLoginName=' + uLoginName + '&uPassword=' + uPassword + '&officesId=' + officesId + '&departId=' + departId + '&uEntryDate=' + uEntryDate + '&uStartDate=' + uStartDate + '&uStopDate=' + uStopDate + '&uName=' + uName + '&uAge=' + uAge + '&uCardId=' + uCardId + '&uPhone=' + uPhone + '&uAddress=' + uAddress + '&uBirthday=' + uBirthday + '&uGender=' + uGender + '&tId=' + tId + 'uModifier=' + uModifier,
                    success: function () {


                        var body = layer.getChildFrame('body');//获取弹窗页面
                        body.find('#uModifier').val(uModifier);
                        console.log(uModifier);
                        body.find('#uId').val(uId);
                        body.find('#uLoginName1').val(uLoginName);
                        body.find('#uPassword').val(uPassword);
                        body.find('#officesId').val(officesId);
                        body.find('#departId').val(departId);
                        body.find('#uEntryDate').val(uEntryDate);
                        body.find('#uStartDate').val(uStartDate);
                        body.find('#uStopDate').val(uStopDate);
                        body.find('#uName').val(uName);
                        body.find('#uAge').val(uAge);
                        body.find('#uCardId').val(uCardId);
                        body.find('#uPhone').val(uPhone);
                        body.find('#uAddress').val(uAddress);
                        body.find('#uBirthday').val(uBirthday);
                        body.find('#uGender').val(uGender);
                        body.find('#tId').val(tId);
                        /* body.find("input[name=user_type][value=1]").attr("checked", data.tId == 1 ? true : false);
                         body.find("input[name=user_type][value=2]").attr("checked", data.tId == 2 ? true : false);
                         body.find("input[name=user_type][value=3]").attr("checked", data.tId == 3 ? true : false);
                         body.find("input[name=user_type][value=4]").attr("checked", data.tId == 4 ? true : false);
                         body.find("input[name=user_type][value=5]").attr("checked", data.tId == 5 ? true : false);
                         body.find("input[name=user_type][value=6]").attr("checked", data.tId == 6 ? true : false);*/
                    }

                });

            }
        });
        //监听状态开关
        form.on('switch(stat)', function (obj) {

            var uId = this.value;
            var boolean = obj.elem.checked;
            if (boolean) {
                var uState = 15;
            } else {
                uState = 16;
            }
            console.log("---" + uId + "---" + uState);
            //修改状态
            $.ajax({
                url: '/JT90/user/updateUserStateById',//地址
                dataType: 'json',//数据类型
                data: {"uId": uId, "uState": uState},
                type: 'GET',//类型
                contentType: 'application/json;charset=UTF-8',
                //请求成功
                success: function (map) {
                    if (map.msg) {
                        if (map.data.uState == 15) {
                            layer.msg("已启用", {
                                icon: 6,
                                time: 1000,
                                end: function () {
                                    var uId = map.data.uId;
                                    var uState = map.data.uState;
                                    var uEntryDate = new Date().toJSON();
                                    var uQuitDate = null;
                                    console.log("离职时间=" + uQuitDate + "---入职时间=" + uEntryDate)
                                    $.ajax({
                                        url: '/JT90/user/updateUserQuitDateByState',//地址
                                        dataType: 'json',//数据类型
                                        data: {
                                            "uId": uId,
                                            "uState": uState,
                                            "uEntryDate": uEntryDate,
                                            "uQuitDate": uQuitDate
                                        },
                                        type: 'GET',//类型
                                        contentType: 'application/json;charset=UTF-8',
                                        //请求成功
                                        success: function (map) {
                                            if (map.msg) {
                                                var uQuitDate = null;
                                                $.ajax({
                                                    url: '/JT90/user/updateUserQuitDateById',//地址
                                                    dataType: 'json',//数据类型
                                                    data: {
                                                        "uId": uId,
                                                        "uQuitDate": uQuitDate
                                                    },
                                                    type: 'GET',//类型
                                                    contentType: 'application/json;charset=UTF-8',
                                                    //请求成功
                                                    success: function (map) {
                                                        if (map.msg) {
                                                            location.reload();
                                                        }
                                                    },
                                                    //失败/超时
                                                    error: function () {
                                                        alert('請求超時');
                                                        //alert(errorThrown);
                                                    }
                                                });
                                            }
                                        },
                                        //失败/超时
                                        error: function () {
                                            alert('請求超時');
                                            //alert(errorThrown);
                                        }
                                    });
                                }
                            })
                            //根据状态修改入职离职时间


                        } else if (map.data.uState == 16) {
                            layer.msg("已停用", {
                                icon: 6,
                                time: 1000,
                                end: function () {
                                    var uId = map.data.uId;
                                    var uState = map.data.uState;
                                    var uQuitDate = new Date().toJSON();
                                    var uEntryDate = null;
                                    console.log("入职时间=" + uEntryDate + "---离职时间=" + uQuitDate)
                                    $.ajax({
                                        url: '/JT90/user/updateUserQuitDateByState',//地址
                                        dataType: 'json',//数据类型
                                        data: {
                                            "uId": uId,
                                            "uState": uState,
                                            "uEntryDate": uEntryDate,
                                            "uQuitDate": uQuitDate
                                        },
                                        type: 'GET',//类型
                                        contentType: 'application/json;charset=UTF-8',
                                        //请求成功
                                        success: function (map) {
                                            if (map.msg) {

                                                location.reload();
                                            }
                                        },
                                        //失败/超时
                                        error: function () {
                                            alert('請求超時');
                                            //alert(errorThrown);
                                        }
                                    });
                                }
                            })
                        }
                    }
                    return false;
                },
                //失败/超时
                error: function () {
                    alert('請求超時');
                    //alert(errorThrown);
                }
            });
        });


        //监听表格复选框选择
        table.on('checkbox(demo)', function (obj) {
            console.log(obj.data)
        });

        //批量删除
        var $ = layui.$, active = {
            getCheckData: function (obj) { //获取选中数据
                var checkStatus = table.checkStatus('test')
                    , data = checkStatus.data;
                if (data == 0) {
                    layer.msg('至少选择一个', {icon: 2});

                    return;
                }
                var checkList = "";
                var uIdList = "";
                if (data.length > 0) {
                    for (var i = 0; i < data.length; i++) {

                        checkList += data[i].uId + ",";
                        uIdList += data[i].tId + ",";


                    }
                    console.log(checkList + "---" + uIdList);
                }
                for (var a = 0; a < data.length; a++) {
                    console.log(a);
                    if (data[a].tId === 6) {
                        layer.open({
                            title: '警告',
                            content: '要删除的用户中有超级管理员，该用户无法删除！'
                        });
                        return;
                    }
                }

                layer.confirm('确认要删除吗？', function () {

                    $.ajax({
                        url: '/JT90/user/deleteUsersById',//地址
                        dataType: 'json',//数据类型
                        data: {"checkList": checkList},
                        type: 'GET',//类型
                        contentType: 'application/json;charset=UTF-8',
                        //请求成功
                        success: function (map) {
                            if (map.msg) {
                                if (map.msg) {
                                    layer.msg('已删除!', {
                                        icon: 1,
                                        time: 1500,
                                        end: function () {
                                            location.reload();
                                        }
                                    });
                                }

                            }

                        }
                    });

                });

            }
        };

        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });


    });

</script>
<script type="text/html" id="barDemo">
    <fieldset class="layui-elem-field site-demo-button">
        {{# if(d.tId === 6){ }}
        <button type="button" class="layui-btn layui-btn-sm layui-btn-normal" lay-event="authority">权限</button>
        <button type="button" class="layui-btn layui-btn-sm" lay-event="edit">编辑</button>
        {{# } else { }}
        <button type="button" class="layui-btn layui-btn-sm layui-btn-normal" lay-event="authority">权限</button>
        <button type="button" class="layui-btn layui-btn-sm" lay-event="edit">编辑</button>
        <button class="layui-btn layui-btn-danger layui-btn-xs layui-btn-sm" lay-event="del">删除</button>
        {{# } }}
    </fieldset>
</script>


</html>